package com.cykj.service.impl;

import com.cykj.mapper.DepartmentMapper;
import com.cykj.pojo.CyDepartment;
import com.cykj.service.DepartmentService;
import com.cykj.util.PageResult;
import com.cykj.util.PageUtils;
import com.cykj.util.ResponseDto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public ResponseDto getInfoById(int id) {
        CyDepartment res = departmentMapper.getInfoById(id);
        return new ResponseDto(1,"查询成功",res);
    }

    @Override
    public ResponseDto getAllDepart(int pageCurr,int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyDepartment> allDepart = departmentMapper.getAllDepart();
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1,"查询成功",pageResult);
    }

    @Override
    public ResponseDto changeStatus(int proStatus, int departId) {
        int i = departmentMapper.changeStatus(proStatus, departId);
        if (i != 0){
            return new ResponseDto(1,"修改成功",i);
        }else {
            return new ResponseDto(0,"修改失败",null);
        }
    }

    @Override
    public ResponseDto changeShow(int proShow, int departId) {
        int i = departmentMapper.changeShow(proShow, departId);
        if (i != 0){
            return new ResponseDto(1,"修改成功",i);
        }else {
            return new ResponseDto(0,"修改失败",null);
        }
    }

    @Override
    public ResponseDto searchDepartInfo(String value,int pageCurr,int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyDepartment> cyDepartments = departmentMapper.searchDepartInfo(value);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1,"查询成功",pageResult);
    }

    @Override
    public ResponseDto alterDepart(int departId, String departName, String departIntro) {
        CyDepartment cyDepartment = departmentMapper.searchAlterDepart(departId, departName, departIntro);
        if (cyDepartment != null){
            return new ResponseDto(2,"修改失败",null);
        }else {
            int i = departmentMapper.alterDepart(departId, departName, departIntro);
            if (i != 0){
                return new ResponseDto(1,"修改成功",null);
            }else {
                return new ResponseDto(0,"修改失败",null);
            }
        }


    }

    @Override
    public ResponseDto deleteDepart(int departId) {
        int i = departmentMapper.deleteDepart(departId);
        if (i != 0){
            return new ResponseDto(1,"删除成功",null);
        }else {
            return new ResponseDto(0,"删除失败",null);
        }
    }

    @Override
    public ResponseDto getAllDepartName() {
        List<String> allDepartName = departmentMapper.getAllDepartName();
        return new ResponseDto(1,"查询成功",allDepartName);
    }

    @Override
    public ResponseDto commitDelete(String checkedDepart) {
            //前端将要删除的细项名称变为字符串，再进行分隔
            String[] split = checkedDepart.split("，");
            int length = split.length;
            int count = 0;
            try {
                for (int i = 0; i < length; i++) {
                    int res = departmentMapper.commitDelete(split[i]);
                    if (res >= 1) {
                        count++;
                    }
                }
                if (count == length) {
                    return new ResponseDto(1, "删除成功", null);
                } else {
                    return new ResponseDto(0, "删除失败", null);
                }
            }catch (Exception e){
                e.printStackTrace();
                throw new RuntimeException();
            }
    }

    @Override
    public ResponseDto selectDepartId(String fatherDepart) {
        CyDepartment cyDepartment = departmentMapper.selectDepartId(fatherDepart);
        System.out.println(fatherDepart);
        if (cyDepartment == null){
            return new ResponseDto(-1,"父级科室不存在",null);
        }else {
            return new ResponseDto(1,"成功",cyDepartment);
        }
    }

    @Override
    public ResponseDto addDepartCommit(String departName, int departmentParentId, String status, String show, String departInfo) {
        if (status.equals("2")){
            status = "0";
        }
        if (show.equals("2")){
            show = "0";
        }
        int i = departmentMapper.addDepartCommit(
                departName, departmentParentId, Integer.parseInt(status), Integer.parseInt(show), departInfo);
        if (i != 0){
            return new ResponseDto(1, "添加科室成功", i);
        }else {
            return new ResponseDto(0, "添加科室失败", null);
        }
    }
}
